﻿using System;
using System.Configuration;
using System.Linq;
using ARSys.Connector;
using ARSys.Entity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Wicresoft.Task.ARNotice;

namespace ARSys.Test
{


    /// <summary>
    ///This is a test class for ARSysProxyTest and is intended
    ///to contain all ARSysProxyTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ARSysProxyTest
    {
        public ARSysProxyTest()
        {
            Environment.CurrentDirectory = @"D:\Users\documents\visual studio 2010\Projects\ARSys.EmailService\Release";

        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for SetEntry
        ///</summary>
        [TestMethod()]
        public void SetEntryTest()
        {

            string server = "vs31srm01";
            string user = "miles.admin";
            string password = "admin";
            using (ARSysProxy target = new ARSysProxy(server, user, password))
            {
                object arobj = new Email()
                {
                    AREntryId = "000000000000006",
                    IsSent = 1,
                };
                target.SetEntry(arobj);
            }
        }
        /// <summary>
        ///A test for SetEntry
        ///</summary>
        [TestMethod()]
        public void SetCreateTest()
        {

            string server = "vs31srm01";
            string user = "miles.admin";
            string password = "admin";
            using (ARSysProxy target = new ARSysProxy(server, user, password))
            {
                object arobj = new Email()
                {
                    IsSent = 1,
                    Comment = "哈罗",
                };
                target.CreateEntry(arobj);
            }
        }

        [TestMethod()]
        public void DeleteAllUserTest()
        {
            TestContext.WriteLine("Begin {0}", DateTime.Now);
            try
            {

                using (ARSysContext dc = new ARSysContext())
                {
                    //for (int i = 0; i < 40; i++)
                    try
                    {
                        using (ARSysProxy px = new ARSysProxy(ARServer, ARUid, ARPwd))
                        {
                            //using (ARTransaction tsc = px.BeginTransaction())
                            //{
                                var source = from ppl in dc.People
                                             where ppl.CIL != ARUid
                                             select ppl;

                                foreach (var ppl in source
                                    //.Take(15000)
                                    )
                                {
                                    //TestContext.WriteLine("Deleting {0}", ppl.Alias);

                                    ppl.ProfileStatus = 5; //mark as deletion
                                    px.SetEntry(ppl);
                                    px.DeleteEntry(ppl);
                                }

                            //    TestContext.WriteLine("Begin commit: {0}", DateTime.Now);
                            //    tsc.Commit();
                            //    TestContext.WriteLine("End commit: {0}", DateTime.Now);
                            //}
                        }
                    }
                    catch (Exception e)
                    {
                        TestContext.WriteLine("Error, {0}", e.ToString());
                        throw;
                    }
                }
            }
            finally
            {
                TestContext.WriteLine("End {0}", DateTime.Now);
            }
        }

        private static readonly string ARServer = ConfigurationManager.AppSettings["ARServer"];
        private static readonly string ARUid = ConfigurationManager.AppSettings["ARUid"];
        private static readonly string ARPwd = ConfigurationManager.AppSettings["ARPwd"];
    }
}
